package com.yihu.cms.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.beans.ResultVO;
import com.yihu.beans.user.Group;
import com.yihu.beans.user.GroupQuery;
import com.yihu.beans.user.PermitQuery;
import com.yihu.beans.user.Priviledge;
import com.yihu.beans.user.ResourceURI;
import com.yihu.beans.user.ResourceURIQuery;
import com.yihu.beans.user.Role;
import com.yihu.beans.user.UserGroupRef;
import com.yihu.cms.service.PriviledgeService;
import com.yihu.cms.service.ResourceURIService;
import com.yihu.cms.service.UserGroupService;

@Controller
public class ResourceURIController extends BaseController{
	

	private ResourceURIService rs = new ResourceURIService();
	@RequestMapping(value = "/user/resource" , method = RequestMethod.POST)
	public @ResponseBody Object addResource(@RequestBody ResourceURI r) {
		log.debug("addResource: "+r.toString());
		ResultVO result= rs.addResource(r);
		log.debug(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/resource/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object updateResource(@RequestBody ResourceURI r,@PathVariable("id") long id) {
		r.setResourceId(id);
		log.debug("updateResource: "+r.toString());
		ResultVO result= rs.updateResource(r);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/resource/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object deleteResource(@PathVariable("id") long id) {
		log.debug("deleteResource: "+id);
		ResultVO result= rs.deleteResource(id);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/user/resource/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getResource(@PathVariable("id") long id) {
		log.debug("getResource id: "+id);
		ResourceURI p = rs.getResource(id);
		return JSON.parseObject(JSON.toJSONString(p), ResourceURI.class);
	}
	
	@RequestMapping(value = "/user/resource/list" , method = RequestMethod.POST)
	// , method = RequestMethod.PATCH
	public @ResponseBody Object getResourcesPaging(@RequestBody ResourceURIQuery query) {
		log.debug("getResourcesPaging "+query.toString());
		String resultJson= rs.getResourceByPage(query);
		return JSON.parse(resultJson);
	}

}
